<template>
	<yplayout v-if="isLoad">
		<template #header="props">
			<div class="flex items-center">
				<i
					class="ri-2x text-gray-500 ml-2"
					:class="props.meta.icon || 'ri-information-line'"
				></i>
				<div class="text-xl px-2" v-html="props.meta.title"></div>
			</div>
		</template>
		<template #list>
			<div class="text-sm text-gray-800">
				<div class="bg-white select-none p-2 space-y-3">
					<div class="flex items-center">
						<div class="w-24 text-right pr-5">企业名称</div>
						<div class="flex-1">
							<ypinput
								v-model="formData.name"
								placeholder="请输入企业名称"
							></ypinput>
						</div>
					</div>
					<div class="flex items-center">
						<div class="w-24 text-right pr-5">法人</div>
						<div class="flex-1">
							<ypinput
								v-model="formData.legal"
								placeholder="请输入法人"
							></ypinput>
						</div>
					</div>
					<div class="flex items-center">
						<div class="w-24 text-right pr-5">营业执照</div>
						<div class="flex-1">
							<ypthumb v-model="formData.thumb"></ypthumb>
						</div>
					</div>

					<div class="flex items-center">
						<div class="w-24 text-right pr-5">联系人</div>
						<div class="flex-1">
							<ypinput
								v-model="formData.contact"
								placeholder="请输入联系人"
							></ypinput>
						</div>
					</div>
					<div class="flex items-center">
						<div class="w-24 text-right pr-5">联系电话</div>
						<div class="flex-1">
							<ypinput
								v-model="formData.phone"
								type="tel"
								placeholder="请输入联系电话"
							></ypinput>
						</div>
					</div>
					<div class="flex items-center">
						<div class="w-24 text-right pr-5">详细地址</div>
						<div class="flex-1 flex space-x-1">
							<ypmselect pid="168" v-model="formData.area"></ypmselect>
							<div class="flex-1">
								<ypinput v-model="formData.address"	placeholder="请输入公司地址"></ypinput>
							</div>
						</div>
					</div>
					<div class="flex items-center">
						<div class="w-24 text-right pr-5">店铺总数</div>
						<div class="flex-1">
							<ypinput
								v-model="formData.totalstores"
								type="number"
								placeholder="请输入店铺总数"
							></ypinput>
						</div>
					</div>
					<div class="flex items-center">
						<div class="w-24 text-right pr-5">备注</div>
						<div class="flex-1">
							<yptextarea
								v-model="formData.intro"
								placeholder="请输入备注"
							></yptextarea>
						</div>
					</div>
				</div>
				<div class="flex justify-center py-3 space-x-2">
					<button class="btn btn-hong" type="button" @click="update">
						<i class="ri-save-line ri-lg pr-1"></i>
						保存
					</button>
					<button class="btn" type="button" @click="router.push('list')">
						<i class="ri-arrow-go-back-line ri-lg pr-1"></i>
						返回
					</button>
				</div>
			</div>
		</template>
	</yplayout>
</template>
<script setup>
	import { useRoute, useRouter } from 'vue-router'
	import { getData, postData, alter } from '@/common/base'
	import { ref, reactive, onMounted } from 'vue'
	const route = useRoute()
	const router = useRouter()
	const isLoad = ref(false)
	const id = route.query
	const formData = reactive({
		name: '',
		contact: '',
		thumb: '',
		phone: '',
		intro: '',
		status: '',
		legal: '',
		address: '',
		area: [],
		totalstores: '',
	})
	const update = async () => {
		const formDataParams = Object.assign({}, formData)
		formDataParams.area = JSON.stringify(formDataParams.area)
		const resp = await postData('company/update', formDataParams)
		if (resp.code) {
			alter({ type: 'alter-success', text: resp.msg })
			router.push('list')
		}
	}

	const read = async () => {
		const resp = await getData('company/getData', id)
		if (resp.code == 1) {
			Object.keys(resp.data).map((k) => {
				formData[k] = resp.data[k]
			})
			isLoad.value = true
		}
	}

	onMounted(() => {
		if (id) {
			read()
		}
	})
</script>
